﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.IO;

using HAUtil;
using HA.Data;

using HA.ModFlow.Domain.DomainLayer;

namespace HA.ModFlow.Application
{

    /// <summary>
    /// 所有项目数据结构相关的服务
    /// </summary>
    public class ProjectService
    {

        protected ProjectService()
        {

        }

        static ProjectService _defaultService;

        public static ProjectService DefaultService
        {
            get
            {

                if (_defaultService == null)
                {

                    _defaultService = new ProjectService();

                }
                return ProjectService._defaultService;
            }

        }




        Project _project;

        public Project Project
        {
            get { return _project; }

        }



        #region 对外的事件模型

        [field: NonSerialized]
        public event ProjHandler ProjChanged;

        [field: NonSerialized]
        public event UIHandler UINeedUpdate;

        protected void AskUIUpdate(UIRefreshArgs args)
        {
            if (UINeedUpdate != null)
            {
                UINeedUpdate(args);

            }
        }


        public event EventHandler<ProjectUIEventArgs> UIUpdatting;

        #endregion

        #region 核心数据部分





        #endregion

        #region 项目管理部分

        protected string projFileName = "";

        private bool m_bNullProj = true;

        /// <summary>
        /// 查看项目是否为关闭状态
        /// </summary>
        public bool BNullProj
        {
            get { return m_bNullProj; }

        }


        public bool Save()
        {
            //如果当前项目为空，则不返回
            if (this.m_bNullProj)
            {
                return false;

            }

           //需要更新project的版本号
            this.UpdateProjectVersion();



            bool b = FileOperations.SaveToBinary(this.projFileName, _project);


            //保存结果数据
            //为兼容就项目（旧项目中所有数据统一保存，现在将结果数据单独提出保存），首先检查是否存在保存结果文件的文件夹
            string path = this.GetProjPath() + this.GetProjName();
            if (Directory.Exists(path))
            {
                string resultDirectory = path + "\\" + "results";
                if (!Directory.Exists(resultDirectory))
                {
                    Directory.CreateDirectory(resultDirectory);
                }
            }

            if (_project != null && _project.Grid3dData != null && _project.Grid3dData.MainGrid != null)
            {
                //保存结果文件到物理文件中
                string resultFile = GetProjPath(ProjPath.RESULTS) + "project.results";

                b = FileOperations.SaveToBinary(resultFile, _project.Grid3dData.MainGrid.DatasetCollecion);

            }
            else 
            {
                //目前项目没有结果数据，则需要清空结果文件夹中的旧的结果文件

                string resultFile = GetProjPath(ProjPath.RESULTS) + "project.results";

                if (File.Exists (resultFile))
                {          
                    File.Delete(resultFile  );
                }
            
            }



            if (ProjChanged != null)
            {
                this.ProjChanged(ProjChangedType.SAVE);
            }

            return b;


        }

        public bool CloseProj()
        {
            projFileName = "";
            this.m_bNullProj = true;
            this._project = null;

            if (ProjChanged != null)
            {
                this.ProjChanged(ProjChangedType.CLOSE);
            }

            return true;

        }

        public bool Open(string projPathName)
        {
            try
            {
                this.projFileName = projPathName;


                _project = (Project)FileOperations.OpenFromBinary(this.projFileName, ref m_bNullProj);

                //这里需要添加关于软件版本号和项目文件版本号的判断问题，  版本不一致时给出提示信息，不需要做其它的纠错操作等
                this.JudgeAppAndProjectVersion();


                //打开结果数据信息

                if (_project != null && _project.Grid3dData != null && _project.Grid3dData.MainGrid != null )
                {
                    //为兼容就项目（旧项目中所有数据统一保存，现在将结果数据单独提出保存），首先检查是否存在保存结果文件的文件夹

                    string path = this.GetProjPath() + this.GetProjName();

                    if (Directory.Exists(path))
                    {
                        string resultDirectory = path + "\\" + "results";
                        if (!Directory.Exists(resultDirectory))
                        {
                            Directory.CreateDirectory(resultDirectory);
                        }
                        
                        string resultFile = GetProjPath(ProjPath.RESULTS) + "project.results";
                        //检查物理文件是否存在
                        if (File.Exists(resultFile))
                        {
                            _project.Grid3dData.MainGrid.DatasetCollecion = (List<DSGrid3dCollecion>)FileOperations.OpenFromBinary(resultFile, ref m_bNullProj);

                        }
                    }
                    //当打开旧项目的时候，可能会导致该字段为null，在此对其判断保证其不能为null
                    if ( _project.Grid3dData.MainGrid.DatasetCollecion==null )
                    {
                        _project.Grid3dData.MainGrid.DatasetCollecion = new List<DSGrid3dCollecion>();                   

                    }
                }



                m_bNullProj = false;

                this.reserveEvents();

                //激活对应的事件
                if (ProjChanged != null)
                {
                    this.ProjChanged(ProjChangedType.OPEN);
                }

                //如果没有结果数据可以表现，（例如打开结果数据过程出错）要清空主程序中的结果数据，以免造成混乱
                if (_project != null && _project.Grid3dData != null && _project.Grid3dData.MainGrid != null&&_project.Grid3dData.MainGrid.DatasetCollecion != null)
                {
                    if (_project.Grid3dData.MainGrid.DatasetCollecion.Count <= 0)
                    {                                                

                        //如果没有结果数据，同时也应该将主程序存储的显示的结果数据清空
                        if (_project.CurentFlowVectorData!=null )
                        {
                            _project.CurentFlowVectorData = null;//向量数据
                        }
                        if (_project.CurrentDSGrid!=null )
                        {
                            _project.CurrentDSGrid = null;//DSgrid3d
                        }
                        if (_project.CurrentDSData!=null )
                        {
                             _project.CurrentDSData = null;//cellcenter数据
                        }                     

                    }

                }
                
                //Console.WriteLine(_mainData.ToString());
                return true;
            }
            catch (Exception ex)
            {
                Console.WriteLine("打开错误：" + ex.Message);
                return false;
            }
        }


        public bool CreateNew(string projPathName)
        {

            _project = new Project();

            this.UpdateProjectVersion();

            projFileName = projPathName;

            //创建对应的工程用文件夹

            this.CreateDirectories();


            this.m_bNullProj = false;



            //Grid3d gdNew = new Grid3d(10, 8, 4, 100, 100, 40, 0, 0, 0, 0);
            //_project.Grid3dData.Add(gdNew);

            //_project.NewModflow();

            //this._project.NewMt3dms();


            //Scatter2D s = new Scatter2D();

            //this._scatter2dData.Datas.Add(s);


            //Coverage c = new Coverage();

            //ConceptualModel cm = new ConceptualModel();

            //_project.MapData.AddCoverage(c);
            //_project.MapData.AddConceptualModel(cm);






            //加入关于测试用 概念建模的数据


            //GridFrame gf = new GridFrame();
            //gf.Name = "GridFrame";


            //_project.MapData.GridFrame = gf;




            //ConceptualModel cm = _project.MapData.NewConcetualModel();
            //Coverage bounC = cm.NewCoverage();

            //bounC.Name = "Boundary";

            //cm.AddCoverage(bounC);


            //Coverage SourceSinkC = cm.NewCoverage();
            //SourceSinkC.Name = "SourceSink";

            //cm.AddCoverage(SourceSinkC);


            //_project.MapData.AddConceptualModel(cm);





            //Well well1 = new Well(1, 2, 1, 10, 8, 4);
            //well1.RechargeRates.Add(1);
            //Well well2 = new Well(3, 4, 2, 10, 8, 4);
            //well2.RechargeRates.Add(2);
            //_project.MfData.SourceSinks.Wells.AddWell(well1);
            //_project.MfData.SourceSinks.Wells.AddWell(well2);
            // _project.MfData.SourceSinks.Wells.Enabled = true;






            //订阅对应的事件
            this.reserveEvents();




            if (ProjChanged != null)
            {
                this.ProjChanged(ProjChangedType.NEW);
            }

            return true;

        }


        /// <summary>
        /// 订阅对应的事件模型
        /// </summary>

        protected void reserveEvents()
        {

            //关于事件订阅的几个基本要求

            //集合事件变更

            //着手建立新的事件体系

            //this._project.Grid3dData.CollectionChanged


            this._project.Grid3dData.CollectionChanged += new EventHandler<CollecionChangedEventArgs>(Grid3dData_CollectionChanged);

            this._project.GridModified += new EventHandler<GridModifiedEventArgs>(_project_GridModified);

            this._project.WaterGridModified += new EventHandler<WaterGridModifiedEventArgs>(_project_WaterGridModified);

            this._project.SCCollectionChanged += new EventHandler<SCColleconChangedEventArgs>(_project_SCCollectionChanged);



            this._project.MapData.GridFrameChanged += new EventHandler<ObjectOperationEventArgs>(MapData_GridFrameChanged);


            this._project.MapData.FOCollecionChanged += new EventHandler<FOCollecionChangedEventArgs>(MapData_FOCollecionChanged);


            this._project.MapData.ConceputalModelCollectionChanged += new EventHandler<CollecionChangedEventArgs>(MapData_ConceputalModelCollectionChanged);

            this._project.MapData.CoverageCollecionChanged += new EventHandler<CoverageCollectionChangedEventArgs>(MapData_CoverageCollecionChanged);

            this._project.AssistData.BitImageChanged += new EventHandler<BitImageCollectionChangedEventArgs>(AssistData_BitImageChanged);



            this._project.Scatter2dData.Scatter2DChanged += new EventHandler<Scatter2DPointsChangedEventArgs>(Scatter2dData_Scatter2DChanged);

            this._project.Scatter2dData.CollectionChanged += new EventHandler<CollecionChangedEventArgs>(Scatter2dData_CollectionChanged);




            // this._project.CurrentDataSetChanged += new EventHandler<CurrentDataSetChangedEventArgs>(_project_CurrentDataSetChanged);

            this._project.CurrentDSChanged += new EventHandler(_project_CurrentDSChanged);
            //this._project.CurrentDSGridChanged += new EventHandler(_project_CurrentDSGridChanged);


            this._project.CurrentFlowVectorDataChanged += new EventHandler(_project_CurrentFlowVectorDataChanged);


            //this._project.DisplayOption.Grid3d.DisplayOptionsChanged += new EventHandler<DisplayOptionsChangedEventArgs>(_project_DisplayOptionsChanged);
            //this._project.DisplayOption.Particles.DisplayOptionsChanged += new EventHandler<DisplayOptionsChangedEventArgs>(_project_DisplayOptionsChanged);
            //this._project.DisplayOption.Modflow.DisplayOptionsChanged += new EventHandler<DisplayOptionsChangedEventArgs>(_project_DisplayOptionsChanged);



            //this._project.DisplayOption.DisplayOptionsChanged += new EventHandler<DisplayOptionsChangedEventArgs>(DisplayOption_DisplayOptionsChanged);


            ////等值线部分做独立的处理吧

            //this._project.DisplayOption.Grid3d.Contour.ContourLinesChanged += new EventHandler(Contour_ContourLinesChanged);

            ////VectorsSetup部分也单独处理

            //this._project.DisplayOption.Grid3d.Vector.VectorsSetupChanged += new EventHandler(Vector_VectorsSetupChanged);



            #region DisplayOption2  二次重构

            this.Project.DisplayOption2.DisplayOptionsSetupChanged += new EventHandler<DisplayOptionsChangedEventArgs>(DisplayOption2_DisplayOptionsChanged);


            this.Project.DisplayOption2.ContourSetupChanged += new EventHandler<ContourSetupChangedEventArgs>(Contour2_ContourLinesChanged);

            
           this._project.DisplayOption2.VectorSetupChanged  += new EventHandler(Vector_VectorsSetupChanged);
            



            #endregion



            //模型本身被删除或者添加时



            this._project.ModFlowChanged += new EventHandler<ModelChangedEventArgs>(_project_ModFlowChanged);

            this._project.MT3dMSChanged += new EventHandler<ModelChangedEventArgs>(_project_MT3dMSChanged);
            
            //插值选项部分
            this._project.InterpolationOption2D.IDWOptionChanged+=new EventHandler(InterpolationOption2D_IDWOptionChanged);
            this._project.InterpolationOption2D.KrigingOptionChanged+=new EventHandler(InterpolationOption2D_KrigingOptionChanged);


            //源汇项的兄弟们

            //这里使用layername 来表示具体的类别，暂时性决定

            ////第一步订阅点，线面源等

            //this._project.MfData.GridReseted += new DefaultHandler(MfData_GridReseted);

            //this._project.MfData.GridData.GridChanged += new DefaultHandler(GridData_GridChanged);


            //this._project.MfData.GridData.WaterGrid.BoundChanged += new DefaultHandler(WaterGrid_BoundChanged);

            //this._project.MfData.GridData.WaterGrid.InitHeadChanged += new DefaultHandler(WaterGrid_HeadChanged);

        }

        void InterpolationOption2D_KrigingOptionChanged(object sender, EventArgs e)
        {
            if(UIUpdatting!=null)
                UIUpdatting(sender,new ProjectUIEventArgs(UIREFRESHTYPE.KRIGINGOPTIONChanged,e));
        }

        void InterpolationOption2D_IDWOptionChanged(object sender, EventArgs e)
        {
            if(UIUpdatting!=null)
                UIUpdatting(sender,new ProjectUIEventArgs(UIREFRESHTYPE.IDWOPTIONChanged,e));
        }

        void Contour2_ContourLinesChanged(object sender, ContourSetupChangedEventArgs e)
        {
            if (UIUpdatting != null)
            {
                UIUpdatting(sender, new ProjectUIEventArgs(UIREFRESHTYPE.ContourlineChanged2, e));
            }

        }

        void DisplayOption2_DisplayOptionsChanged(object sender, DisplayOptionsChangedEventArgs e)
        {
            if (UIUpdatting != null)
            {
                UIUpdatting(sender, new ProjectUIEventArgs(UIREFRESHTYPE.DISPLAYOPTIONS2, e));
            }
        }








        void _project_CurrentDSGridChanged(object sender, EventArgs e)
        {
            if (UIUpdatting != null)
            {
                UIUpdatting(sender, new ProjectUIEventArgs(UIREFRESHTYPE.CurrentDSGridChanged, e));
            }

        }

        void Contour_ContourLinesChanged(object sender, EventArgs e)
        {
            if (UIUpdatting != null)
            {
                UIUpdatting(sender, new ProjectUIEventArgs(UIREFRESHTYPE.ContourlineChanged, e));
            }

        }




        void Vector_VectorsSetupChanged(object sender,EventArgs e)
        {
            if (UIUpdatting !=null )
            {
                UIUpdatting(sender ,new ProjectUIEventArgs (UIREFRESHTYPE .VectorsSetupChanged ,e));
            }
        
        }



        void DisplayOption_DisplayOptionsChanged(object sender, DisplayOptionsChangedEventArgs e)
        {
            if (UIUpdatting != null)
            {
                UIUpdatting(sender, new ProjectUIEventArgs(UIREFRESHTYPE.DISPLAYOPTIONS, e));
            }
        }

        void Scatter2dData_CollectionChanged(object sender, CollecionChangedEventArgs e)
        {
            if (UIUpdatting != null)
            {
                UIUpdatting(sender, new ProjectUIEventArgs(UIREFRESHTYPE.SCATTER2DCOLLECTION, e));
            }
        }

        void Scatter2dData_Scatter2DChanged(object sender, Scatter2DPointsChangedEventArgs e)
        {
            if (UIUpdatting != null)
                UIUpdatting(sender, new ProjectUIEventArgs(UIREFRESHTYPE.SCATTER2DSHAPE, e));
        }


        void _project_CurrentDSChanged(object sender, EventArgs e)
        {
            if (UIUpdatting != null)
            {
                UIUpdatting(sender, new ProjectUIEventArgs(UIREFRESHTYPE.CURRENTDATASETCHANGED, e));
            }

        }

        void _project_CurrentFlowVectorDataChanged(object sender, EventArgs e)
        {
            if (UIUpdatting != null)
            {
                UIUpdatting(sender, new ProjectUIEventArgs(UIREFRESHTYPE.CURRENTFLOWVECTORDATACHANGED , e));
            }

        }

        void _project_MT3dMSChanged(object sender, ModelChangedEventArgs e)
        {
            if (UIUpdatting != null)
            {
                UIUpdatting(sender, new ProjectUIEventArgs(UIREFRESHTYPE.MT3DMSChanged, e));
            }
        }

        void _project_ModFlowChanged(object sender, ModelChangedEventArgs e)
        {
            if (UIUpdatting != null)
            {
                UIUpdatting(sender, new ProjectUIEventArgs(UIREFRESHTYPE.MODFLOWChanged, e));
            }
        }

       

        void AssistData_BitImageChanged(object sender, BitImageCollectionChangedEventArgs e)
        {
            if (UIUpdatting != null)
            {
                UIUpdatting(sender, new ProjectUIEventArgs(UIREFRESHTYPE.BitImageCollectionChanged, e));
            }
        }

        void MapData_CoverageCollecionChanged(object sender, CoverageCollectionChangedEventArgs e)
        {
            if (UIUpdatting != null)
            {
                UIUpdatting(sender, new ProjectUIEventArgs(UIREFRESHTYPE.CoverageCollecionChanged, e));

            }
        }

        void MapData_ConceputalModelCollectionChanged(object sender, CollecionChangedEventArgs e)
        {
            if (UIUpdatting != null)
            {
                UIUpdatting(sender, new ProjectUIEventArgs(UIREFRESHTYPE.ModflowCollecionChanged, e));

            }
        }

        void MapData_FOCollecionChanged(object sender, FOCollecionChangedEventArgs e)
        {
            if (UIUpdatting != null)
            {
                UIUpdatting(sender, new ProjectUIEventArgs(UIREFRESHTYPE.FOCHANGED, e));

            }

        }






        //void _project_CurrentDataSetChanged(object sender, CurrentDataSetChangedEventArgs e)
        //{

        //    if (UIUpdatting != null)
        //    {
        //        UIUpdatting(sender, new ProjectUIEventArgs(UIREFRESHTYPE.CURRENTDATASETCHANGED, e));

        //    }
        //}



        void MapData_GridFrameChanged(object sender, EventArgs e)
        {
            if (UIUpdatting != null)
            {
                UIUpdatting(sender, new ProjectUIEventArgs(UIREFRESHTYPE.GRIDFRAMEMODIFIED, e));

            }

        }

        void _project_SCCollectionChanged(object sender, SCColleconChangedEventArgs e)
        {
            if (UIUpdatting != null)
            {

                UIUpdatting(sender, new ProjectUIEventArgs(UIREFRESHTYPE.SCCOLLECION, e));

            }

        }

        void _project_WaterGridModified(object sender, WaterGridModifiedEventArgs e)
        {
            if (UIUpdatting != null)
            {
                UIUpdatting(sender, new ProjectUIEventArgs(UIREFRESHTYPE.WATERGRIDMODIFIED, e));

            }
        }

        void _project_GridModified(object sender, GridModifiedEventArgs e)
        {
            if (UIUpdatting != null)
            {
                UIUpdatting(sender, new ProjectUIEventArgs(UIREFRESHTYPE.GRIDMODIDIFIED, e));

            }

        }

        void Grid3dData_CollectionChanged(object sender, CollecionChangedEventArgs e)
        {

            if (UIUpdatting != null)
            {
                UIUpdatting(sender, new ProjectUIEventArgs(UIREFRESHTYPE.GRIDRESET, e));

            }



        }








        /// <summary>
        /// 创建对应的项目文件夹
        /// </summary>
        protected void CreateDirectories()
        {
            string path = this.GetProjPath() + this.GetProjName();

            //如果存在则删除其及子文件夹
            if (Directory.Exists(path))
            {
                Directory.Delete(path, true);
            }

            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);

                //string exeDir = System.AppDomain.CurrentDomain.BaseDirectory + @"exe\";
                //CopyDirectory(exeDir, path + @"\");

                DirectoryInfo dirInfo = new DirectoryInfo(path);

                dirInfo.CreateSubdirectory("solutions");
                dirInfo.CreateSubdirectory("resources");

                dirInfo.CreateSubdirectory("results");


                //dirInfo = new DirectoryInfo(path + @"\InputFile");
                //dirInfo.CreateSubdirectory("akterm");
                //dirInfo.CreateSubdirectory("image");

            }
        }

        /// <summary>
        /// 拷贝文件夹
        /// </summary>
        /// <param name="srcdir"></param>
        /// <param name="desdir"></param>
        private void CopyDirectory(string srcdir, string desdir)
        {
            string folderName = srcdir.Substring(srcdir.LastIndexOf("\\") + 1);

            string desfolderdir = desdir + "\\" + folderName;

            if (desdir.LastIndexOf("\\") == (desdir.Length - 1))
            {
                desfolderdir = desdir + folderName;
            }
            string[] filenames = Directory.GetFileSystemEntries(srcdir);

            foreach (string file in filenames)// 遍历所有的文件和目录
            {
                if (Directory.Exists(file))// 先当作目录处理如果存在这个目录就递归Copy该目录下面的文件
                {

                    string currentdir = desfolderdir + "\\" + file.Substring(file.LastIndexOf("\\") + 1);
                    if (!Directory.Exists(currentdir))
                    {
                        Directory.CreateDirectory(currentdir);
                    }

                    CopyDirectory(file, desfolderdir);
                }

                else // 否则直接copy文件
                {
                    string srcfileName = file.Substring(file.LastIndexOf("\\") + 1);

                    srcfileName = desfolderdir + "\\" + srcfileName;


                    if (!Directory.Exists(desfolderdir))
                    {
                        Directory.CreateDirectory(desfolderdir);
                    }


                    File.Copy(file, srcfileName);
                }
            }
        }


        #endregion




        #region 项目辅助应用部分

        /// <summary>
        /// 返回工程所在目录,这里返回的值不包含工程的文件名子文件夹
        /// </summary>
        /// <returns></returns>
        public string GetProjPath()
        {

            return Path.GetDirectoryName(projFileName) + @"\";


        }


        /// <summary>
        /// 提供基本的目录索引服务
        /// </summary>
        /// <returns></returns>
        public string GetProjPath(ProjPath pathType)
        {
            switch (pathType)
            {
                case ProjPath.WORKPATH:
                    return this.GetProjPath() + Path.GetFileNameWithoutExtension(this.projFileName) + "\\";

                case ProjPath.RESOURCE:
                    return this.GetProjPath(ProjPath.WORKPATH) + @"resources\";

                case ProjPath.SOLUTION:

                    return this.GetProjPath(ProjPath.WORKPATH) + @"solutions\";

                case ProjPath .RESULTS :

                    return this.GetProjPath(ProjPath.WORKPATH) + @"results\";

                case ProjPath.CCF:

                    return this.GetProjPath(ProjPath.WORKPATH) + @"solutions\ccf\";


                default:
                    break;
            }

            return "";

        }


        /// <summary>
        /// 返回MT3D各个污染物对应的存放生成文件的文件夹的  绝对路径
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public string GetMT3DResultDirectoryPath(int index)
        {

            string path = this.GetProjPath(ProjPath.WORKPATH) + @"solutions\";

            string directoryName = HA.MT3DMS.FileLayer.FileNameConfigHelper.GetMT3DResultDirectoryName(index);
           
            if (directoryName.Trim() == "")
            {
                return ""; 
            }
            else
            {
                path = path + directoryName;
           
            }

            return path;


        }
        /// <summary>
        /// 返回MT3D各个污染物对应的存放生成文件的文件夹的  名称
        /// </summary>
        /// <param name="index"></param>
        /// <returns></returns>
        public string GetMT3DResultDirectoryName( int index)
        {
            string directoryName =HA.MT3DMS .FileLayer .FileNameConfigHelper .GetMT3DResultDirectoryName (index );

            if (directoryName.Trim() == "")
            {
                return "";
            }

            return directoryName;
        
        }

        /// <summary>
        /// 返回MT3D各个污染物对应的 配置文件的名称
        /// <param name="index"></param>
        /// <returns></returns>
        public string GetMT3DResultPmIniName(int index)
        {
            string pmName = HA.MT3DMS.FileLayer.FileNameConfigHelper.GetMT3DResultPmIniName(index ); ;
           
            if (pmName .Trim ()=="")
            {
                return "";

            }

            return pmName;
        
        }

        /// <summary>
        /// 返回工程主文件的文件地址
        /// </summary>
        /// <returns></returns>
        public string GetProjMainFile()
        {
            return this.projFileName;
        }

               



        /// <summary>
        /// 返回不包含文件后缀名的工程名称,返回不具有扩展名的指定路径字符串的文件名。
        /// </summary>
        /// <returns></returns>
        public string GetProjName()
        {
            
            return Path.GetFileNameWithoutExtension(projFileName);

        }

        /// <summary>
        /// 判断应用程序的版本号和项目Project的版本号
        /// 不一致的 时候给出提示信息
        /// </summary>
        private void JudgeAppAndProjectVersion()
        {
            if (_project != null)
            {
                //该比较方法仿照启动应用程序的时候，小齐写的程序版本号验证的逻辑，   嘿嘿

                string projectVer = _project.ProjectVersion;//项目的版本号

                string appVer = "";//应用程序的版本号



                int proVersion = 0;
                int appVersion = 0;

                try
                {

                    appVer = GetAssemblyVersion();
    

                }
                catch
                {
                    appVer = "1.0.0.0";

                }
                try
                {


                    proVersion = HANetwork.Network.DefaultNetWork.AuthenticationNetInfo.ConvertVersion(projectVer);
                    appVersion = HANetwork.Network.DefaultNetWork.AuthenticationNetInfo.ConvertVersion(appVer);

                    if (proVersion > appVersion)
                    { 
                        //项目的版本号高于应用程序的版本号

                        System.Windows.MessageBox.Show("程序的版本号：" + appVer + "\n\r" + "项目的版本号： " + projectVer + "\n\r" + "您要打开的项目的版本号高于您使用的软件的版本号，可能会导致部分数据丢失！", "提示");
                    }
                    else if (proVersion < appVersion)
                    { 
                        //项目的版本号低于应用程序的版本号

                        System.Windows.MessageBox.Show("程序的版本号：" + appVer + "\n\r" + "项目的版本号： " + projectVer + "\n\r" + "您要打开的项目的版本号低于您使用的软件的版本号！", "提示");
                    
                    }



                }
                catch
                {
                    //呵呵 欺骗一下上帝  打酱油了
                 
                }
     

            }
        
        }
        /// <summary>
        /// 更新项目的版本号
        /// </summary>
        private void UpdateProjectVersion()
        {
            if (_project!=null )
            {

                //记录更新project的版本信息
                //哈哈，加个try catch吧，我怕别人提供的这个方法不靠谱

                try
                {
                    

                    _project.ProjectVersion = GetAssemblyVersion();
    

                }
                catch 
                {
                    _project.ProjectVersion = "1.0.0.0";
                 
                }


            }
        
        
        
        }

        /// <summary>
        /// 取得程序版本号  小齐提供
        /// </summary>
        /// <returns></returns>
        private string GetAssemblyVersion()
        {

            //_project.ProjectVersion = HANetwork.Network.DefaultNetWork.AuthenticationNetInfo.GetComputerVersion();

            //_project.ProjectVersion = System.Reflection.Assembly.GetExecutingAssembly().GetName().Version.ToString();


            string path = AppDomain.CurrentDomain.BaseDirectory + "HA.ModFlow.Presentation.exe";
            System.Reflection.Assembly assembly = System.Reflection.Assembly.LoadFile(path);
            System.Reflection.AssemblyName assemblyName = assembly.GetName();

            return assemblyName.Version.ToString();// HANetwork.Network.DefaultNetWork.AuthenticationNetInfo.GetComputerVersion();
            
        }



        #endregion

        #region 功能函数






        #endregion


    }
}
